<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\Income;
use App\Models\Profit;
use App\Models\Trade;
use App\Models\User;
use Illuminate\Http\Request;
use Excel;
use DB;
use HttpRequest;
use Curl;
use HttpClient;
use Illuminate\Support\Facades\Mail;


class ConsoleController extends Controller
{

    public $ret=array();

    /**
     * 计算代理收入
     */

    public function index()
    {
        $data = DB::table("trades")->where("type","!=","balance")->whereBetween("created_at",[date("Y-m-d"),date("Y-m-d",time()+24*3600)])->get();
        $time= date("Y-m-d H:i:s");
        $year=date("Y");
        $month=date("m");
        $mm=array();
        foreach ($data as $val){
            $m= -$val->profit;
            $info = User::where("tranid",$val->tranid)->first(["sorts"]);
            $sorts = $info->sorts;
            $aid= explode("-",$sorts);

            unset($aid[0]);
            $list = Admin::whereIn("id",$aid)->get(["percent"]);//查看代理们的提成
//            dd($list);
            foreach ($list as $key =>$v){
                if(isset($list[$key+1]->percent)){//判断有没有下级代理
                    if($list[$key+1]->percent == $v->percent){//如果下级代理和当前代理提成相同  当前代理收入为0
//                        $income=0;
//                        $mm[]=array('aid'=>$aid[$key+1],'tid'=>$val->id,'percent'=>$v->percent,'money'=>'0','next'=>$list[$key+1]->percent,'created_at'=>$time);
                    }else{//如果下级代理和当前代理提成不相同，当前代理收入为 提成-下级提成
                        $income=($v->percent - $list[$key+1]->percent)*$m/100;
                        $mm[]=array('aid'=>$aid[$key+1],'tid'=>$val->id,'percent'=>$v->percent,'created_at'=>$time,'next'=>$list[$key+1]->percent,'money'=>$income);
                    }
                }else{
                    $income=intval($v->percent)*$m/100;
                    $mm[]=array('aid'=>$aid[$key+1],'tid'=>$val->id,'percent'=>$v->percent,'created_at'=>$time,'next'=>0,'money'=>$income);
                    Admin::where("id",$aid[$key+1])->increment("totlemoney",$m);
                }

                Admin::where("id",$aid[$key+1])->increment("money",$income);
            }

            //将受益算入  收支明细
            $pcount =  Profit::where(["year"=>$year,'month'=>$month])->count();
            if($pcount){
                $pro = Profit::where(["year"=>$year,'month'=>$month])->first();
                $pro->profit=$pro->profit + $m;
                $pro->pay=$pro->pay + $m*$list[0]->percent/100;
                $pro->save();

            }else{
                $pro= new Profit();
                $pro->year=$year;
                $pro->month=$month;
                $pro->profit=$m;
                $pro->pay=$m*$list[0]->percent/100;
                $pro->save();


            }


        }
        Income::insert($mm);
    }

    public function reset(){
       $list =  Admin::get(['id','totlemoney']);

        $this->setlog(json_encode($list),'系统自动重置代理业绩');

        DB::table("admin")->update(['totlemoney'=>'0']);
    }


    public function setlog($data,$content){
        $insert=array(
            'uid'=>'0',
            'username'=>'系统',
            'ip'=>'0.0.0.0',
            'type'=>'2',
            'content'=>$content,
            'data'=>$data,
            'created_at'=>date("Y-m-d H:i:s")
        );
        DB::table("action_log")->insert($insert);
    }

}